concerto.admin.codeGenerator = {
	contentFields:[],
	usersFields:[],
	// Set data using Concerto data fields
	setData:function(data) {
	
	},
	initDom:function() {
	
	},
	generateContentQuery:function() {
	
	},
	generateUsersQuery:function() {
	
	},
	generateFeedbackQuery:function() {
	
	},
	generateCustomFunction:function() {
	
	}
	
	var parameterSelect = $('<select/>').attr('id', 'dialog-option-select').change(function() {
				var selected = $(this).find(':selected');
				appliedParameters.find('.selected').removeClass('selected');
				$(this).data('currentField', '');
				dialogForm.html('').slideUp(100, function() {
					if(selected.data('secondaryFields')) {
						for(var n=0;n<selected.data('secondaryFields').length;n++) {
							var field = new concertoDesigner.formField(selected.data('secondaryFields')[n]);
							field.appendTo(dialogForm);
							field.initSpecials();
						}
						dialogForm.slideDown(100);
					}
				});

			});
			
			var defaultParam = $('<option/>').val('').html('Select a parameter').appendTo(parameterSelect);
			for(var i in concertoDesigner.data.query.content) {
				var info = concertoDesigner.data.query.content[i];
				var group = $('<optGroup/>').attr('label', info.label).appendTo(parameterSelect);
				for(var n in concertoDesigner.data.query.content[i].options) {
					var optionInfo = concertoDesigner.data.query.content[i].options[n];
					var secondaryFields = false;
					if(optionInfo.secondaryFields) {
						secondaryFields = optionInfo.secondaryFields;
					}
					$('<option/>').val(optionInfo.php).html(optionInfo.display).data('secondaryFields', secondaryFields).appendTo(group);
				}
				
			}
			if(params.parameters) {
				for(var z=0;z<params.parameters.length;z++) {
					console.log(parameterSelect.find('[value=' + params.parameters[z].param + ']'));
					parameterSelect.find('[value=' +params.parameters[z].param + ']').attr('disabled', 'disabled');
					var li = $('<li/>').html(params.parameters[z].html).data('data', params.parameters[z]).appendTo(appliedParameters).click(function() {
						
						appliedParameters.find('.selected').removeClass('selected');
						$(this).addClass('selected');
						parameterSelect.data('currentField', $(this));
						var data = $(this).data('data');
						var param = data.param;
						var fields = [];
						dialogForm.html('').slideUp(100, function() {
							parameterSelect.val(param);
							if(parameterSelect.find(':selected').data('secondaryFields')) {
								for(var n=0;n<parameterSelect.find(':selected').data('secondaryFields').length;n++) {
									var field = new concertoDesigner.formField(parameterSelect.find(':selected').data('secondaryFields')[n]);
									field.appendTo(dialogForm);
									fields.push(field);
								}
								// Set default values and then initSpecials();
								for(var x=0;x<data.fields.length;x++) {
									var field = data.fields[x];
									console.log('Setting ' + field.name + ' to ' + field.val);
									dialogForm.find('[name=' + field.name + ']').val(field.val);
								}
								for(var y=0;y<fields.length;y++) {
									fields[y].initSpecials();
								}
								dialogForm.slideDown(100);
							}
						});
					});
					li.children('.remove-parameter').click(function(e) {
						e.stopPropagation();
						
						if(parameterSelect.find(':selected').val() == $(this).parent('li').data('data').param) {
							parameterSelect.val('');
							dialogForm.html('').slideUp(100);
						}
						parameterSelect.find('[value=' + $(this).parent('li').data('data').param + ']').attr('disabled', false);
						
						$(this).parent('li').fadeOut(100, function() {
							$(this).remove();
						});
					});
				}
				appliedParameters.children('li').each(function() {
					parameterSelect.find('[value=' + $(this).data('data').param + ']').attr('disabled', 'disabled');
				});
			}
			parameterSelect.appendTo(formItems);
			
			dialogForm.appendTo(formItems);
			// Add buttons
			if(!params.submitVal) params.submitVal = 'Submit';
			if(!params.cancelVal) params.cancelVal = 'Cancel';
			$('<button/>').html(params.cancelVal).click(function() {
				$('#dialog').fadeOut(100);
				$('#mask').fadeOut(100);
			}).appendTo(buttons);
			
			// Add new parameter button
			$('<button/>').html('Apply Parameter').appendTo(buttons).click(function() {
				appliedParameters.children('.selected').removeClass('selected');
				// Add a list item to the right side with all the values so it can go back and forth and also be parsed by the PHP at the end
				if(parameterSelect.data('currentField')) {
					var parameter = parameterSelect.data('currentField');
				}
				else {
					var parameter = $('<li/>');
				}
				parameter.html('<b>' + parameterSelect.find(':selected').html() + '</b>');
				var data = {
					fields:[]
				};
				data.param = parameterSelect.find(':selected').val();
				parameterSelect.find(':selected').attr('disabled', 'disabled');
				// Pass all form data to the li
				formItems.find('input').each(function() {
					if($(this).attr('name')) {
						parameter.append(' [' + $(this).val() + ']');
						data.fields.push({name:$(this).attr('name'), val:$(this).val()});
					}
				});
				formItems.find('select').each(function() {
					if($(this).attr('name')) {
						parameter.append(' [' + $(this).val() + ']');
						data.fields.push({name:$(this).attr('name'), val:$(this).val()});
					}
				});
				formItems.find('textarea').each(function() {
					if($(this).attr('name')) {
						parameter.append(' [' + $(this).val() + ']');
						data.fields.push({name:$(this).attr('name'), val:$(this).val()});
					}
				});
				parameter.data('data', data);
				var remove = $('<a/>').html('X').addClass('remove-parameter').appendTo(parameter).click(function(e) {
					e.stopPropagation();
					
					if(parameterSelect.find(':selected').val() == $(this).parent('li').data('data').param) {
						parameterSelect.val('');
						dialogForm.html('').slideUp(100);
					}
					parameterSelect.find('[value=' + $(this).parent('li').data('data').param + ']').attr('disabled', false);
					
					$(this).parent('li').fadeOut(100, function() {
						$(this).remove();
					});
				});
				parameter.appendTo(appliedParameters);
				parameter.unbind('click');
				parameter.click(function() {
					appliedParameters.find('.selected').removeClass('selected');
					$(this).addClass('selected');
					parameterSelect.data('currentField', $(this));
					var data = $(this).data('data');
					var param = data.param;
					var fields = [];
					dialogForm.html('').slideUp(100, function() {
						parameterSelect.val(param);
						if(parameterSelect.find(':selected').data('secondaryFields')) {
							for(var n=0;n<parameterSelect.find(':selected').data('secondaryFields').length;n++) {
								var field = new concertoDesigner.formField(parameterSelect.find(':selected').data('secondaryFields')[n]);
								field.appendTo(dialogForm);
								fields.push(field);
							}
							// Set default values and then initSpecials();
							for(var x=0;x<data.fields.length;x++) {
								var field = data.fields[x];
								dialogForm.find('[name=' + field.name + ']').val(field.val);
							}
							for(var y=0;y<fields.length;y++) {
								fields[y].initSpecials();
							}
							dialogForm.slideDown(100);
						}
					});
				});
				// Now clear the left side so they can apply a new parameter
				parameterSelect.val('');
				dialogForm.slideUp(100, function() {
					$(this).html('');
				});
			});
			
			// Submit button
			$('<button/>').html('Finish & Insert').click(function() {
				if(appliedParameters.children('li').length) {
					// Compile all the data
					var compiled = [];
					appliedParameters.children('li').each(function() {
						var data = $(this).data('data');
						data.html = $(this).html();
						compiled.push(data);
					});
					var options = {
						elementType:'query',
						queryType:'content',
						parameters:compiled
					};
					concertoDesigner.actions.applyDynamicProperty(options);
					$('#dialog').fadeOut(100);
					$('#mask').fadeOut(100);
				}
				else {
					message.html('You must apply at least one parameter.').slideDown(100, function() {
						setTimeout(function() {
							message.slideUp(100);
						}, 2000);
					});
				}
			}).appendTo(buttons);
};